<?php

$c = include(__DIR__."/../config.php");

require_once __DIR__."/../code/Helper.php";

require_once __DIR__."/../app/DeviceController.php";

use code\FileTools;

/**
 * 初始化 swoole 服务
 */
$server = new swoole_server($c['SWOOLE_SERVER'], $c['SWOOLE_PORT']);

/**
 * 初始化文件工具类
 */
$file = new FileTools();

/**
 * 配置 心跳维持 （30秒检查一次，90秒无信息后删除连接）
 */
$server->set(array(
    'heartbeat_check_interval' => 30,
    'heartbeat_idle_time' => 90,
));

$server->on('connect', function ($server, $fd) {
    echo "SERVER ".date('H:i:s')."\n connection open: {$fd}\n";
});

$server->on('receive', function ($server, $fd, $reactor_id, $data) use ($file, $c) {
	$device = new DeviceController($server, $file, $c);
	$device->control($data, $fd);
	unset($device);
});

$server->on('close', function ($server, $fd) {
    echo "SERVER ".date('H:i:s')."\n connection close: {$fd}\n";
});

echo "Smart device server start\n";

$server->start();